#include "saveleveldialog.h"
#include "buttonitem.h"
#include "inputbox.h"

SaveLevelDialog::SaveLevelDialog(const QString &imgName,
                                 const QPoint &leftTop,
                                 const QPoint &rightBottom,
                                 QGraphicsScene *scene,
                                 QGraphicsItem *parent) :
    DialogItem(imgName, leftTop, rightBottom, scene, parent)
{
    saveButton = new ButtonItem(":/dialog-savebutton", QPoint(219, 294),
                                QPoint(295, 355), scene, this);
    cancelButton = new ButtonItem(":/dialog-cancelbutton", QPoint(66, 279),
                                  QPoint(170, 349), scene, this);
//    inputBox = new QGraphicsTextItem("testing", this, scene);
//    inputBox->setPos(86, 200);
//    inputBox->setFont(QFont("Janis", 22));
//    inputBox->setTextWidth(210);
    inputBox = new InputBox("testing", QPointF(86, 200), this, scene, QFont("Janis", 22), 210);

    connect(saveButton, SIGNAL(clicked(ButtonItem*)), this, SLOT(buttonClicked(ButtonItem*)));
    connect(cancelButton, SIGNAL(clicked(ButtonItem*)), this, SLOT(buttonClicked(ButtonItem*)));
}

void SaveLevelDialog::buttonClicked(ButtonItem *who)
{
    if (who == saveButton)
    {
        setVisible(false);
        emit saveClicked(inputBox->toPlainText());
    }
    else if (who == cancelButton)
    {
        setVisible(false);
        emit dialogClosed();
    }
    else if (who == closeButton)
        DialogItem::buttonClicked(who);

}

void SaveLevelDialog::popup()
{
    DialogItem::popup();
    inputBox->grabKeyboard();
}
